<?php
namespace app\api\model;
use think\Model;
class Menus extends Model
{
    protected $schema = [
        'id'            => 'int',
        'title'         => 'string',
        'is_sub'        => 'int',
        'parent'        => 'int',
        'index'         => 'string',
    ];
    
    public static function getJsonMenus($role)
    {
        $top_menus = Menus::field(['title', 'is_sub', 'index', 'path', 'role'])->where('parent', -1)->where('role', '>', $role)->select();
        foreach($top_menus as $top_item){
            if($top_item['is_sub']){
                $child_menus = self::getChildMenu($top_item['index']);
                $top_item['menus'] = $child_menus;
            }
        }
        return $top_menus;
    }
    protected static function getChildMenu($parent_index){
        $child_menus = self::where('parent', $parent_index)->select();
        foreach($child_menus as $child_item){
            if($child_item['is_sub']){
                $menus = self::getChildMenu($child_item['index']);
                $child_item['menus'] = $menus;
            }
        }
        return $child_menus ;
    }
}
